Controlling Text Overflow in CSS
When text content exceeds the width of a container, CSS provides properties to control how the overflow behaves. This is essential for maintaining clean layouts, especially for fixed-width UI elements like cards, buttons, or table cells.
overflow: hidden; – Hides any text that exceeds the container boundaries.
overflow: scroll; – Adds scrollbars so users can scroll to see hidden content.
overflow: auto; – Adds scrollbars only when content overflows.
text-overflow: ellipsis; – Displays an ellipsis (…) for clipped text; works only with overflow: hidden and white-space: nowrap.
white-space: nowrap; – Prevents text from wrapping to the next line, required for single-line ellipsis.
The first example truncates text with an ellipsis when it overflows the fixed-width container. The second example allows scrolling when content exceeds the width or height of the container.
Use text-overflow: ellipsis for single-line text that must fit within a container.
For multi-line content, consider overflow: auto or expandable sections.
Always test text overflow on different screen sizes for responsiveness.
Combine with white-space and width to control layout precisely.